package com.shd.day01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author S5343
 * @date 2018年7月20日下午10:40:38
 */
public class AppleFilter {

	/**
	 * 颜色筛选器
	 * 
	 * @param apples
	 * @param color
	 * @return
	 */
	public static List<Apple> findGreenApple(List<Apple> apples, String color) {
		List<Apple> list = new ArrayList<>();
		for (Apple apple : apples) {
			if (color.equals(apple.getColor())) {
				list.add(apple);
			}
		}
		return list;
	}

	// -------利用策略模式-------------------------------------------------------
	@FunctionalInterface
	public interface AFilter {
		boolean filter(Apple apple);
	}

	public static List<Apple> findApple(List<Apple> apples, AFilter filter) {
		List<Apple> list = new ArrayList<>();
		for (Apple apple : apples) {
			if (filter.filter(apple)) {
				list.add(apple);
			}
		}
		return list;
	}

	public static class Green150Filter implements AFilter {

		@Override
		public boolean filter(Apple apple) {
			return (apple.getColor().equals("green") && apple.getWeight() >= 150);
		}

	}

	// -------end-----------------------------------------------------------------
	public static void main(String[] args) throws InterruptedException {
		List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("red", 200), new Apple("green", 200));
		List<Apple> greenApples = findApple(list, new Green150Filter());
		System.out.println(greenApples);

		List<Apple> lambdaResult = findApple(list, apple -> apple.getColor().equals("green"));
		System.out.println(lambdaResult);
		new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName());

			}
		}).start();

		new Thread(() -> System.out.println(Thread.currentThread().getName())).start();
	}

}
